import 'package:get/get.dart';
import 'package:isar/isar.dart';
import 'package:path_provider/path_provider.dart';

import '../MedicineM/medicine_model.dart';
import '../MedicineM/medicine_service.dart';

class MedicineListVM extends GetxController {
  final RxList<Medicine> medicines = RxList<Medicine>();
  get medicineList => medicines;

  final IsarService _isarService = IsarService();
  late Future<Isar> _isarInstance;

  @override
  onInit() {
    super.onInit();
    _isarInstance = _isarService.isarInstance;
    getMedicines();
  }
  @override
  onClose() {
    super.onClose();
    _isarService.closeIsar();
  }

  getMedicines() async {
    try {
      final isar = await _isarInstance;
      medicines.value = await isar.medicines.where().findAll();
      update();
    } catch (e) {
      print(e);
    }
  }
}